<?php
import ( 'Behavior', LIB_PATH . 'Behavior' );

class LottoBehavior extends Behavior {
	
	private $_description = array ('1' => '抽奖成功', '-1' => '金币不足10个', '-2' => '今天已经抽奖10次' );
	
	# 用户信息
	private $User;
	
	# 奖品元素概率表
	private $items;
	
	# 奖品概率精确度
	private $accurate = 100;
	
	# 抽奖一次扣去10金币
	private $singleGolds = 10;
	
	# 每天抽奖上限次数
	private $maxTimes = 10;
	
	# 返回状态
	private $_rstatus;
	
	private $prize;
	
	public function run($params) {
		# step.1 初始化
		$this->init ( $params ['userinfo'] );
		
		# step.2 检查权限
		$this->checkRight ();
		
		# step.3 抽奖
		$this->onLotto ();
		
		# step.4 写入日志
		$this->onRecord ();
	}
	
	# 初始化
	private function init($userinfo) {
		$this->User = $userinfo;
	}
	
	# 检查权限
	private function checkRight() {
		$result = $this->_checkGolds (); # TODO 金币负数检查
		
		if ($result == 0)
			$result = $this->_checkMaxTimes ();
		$this->_return ( $result );
	}
	
	private function _checkGolds() {
		if($this->User->u_lock_auid){
			$lock_auction = getInfoById('PrizeAuction', $this->User->u_lock_auid);
			$usable_golds = $this->User->u_golds - $lock_auction['au_golds'];
		}else{
			$usable_golds = $this->User->u_golds;
		}
		return ($usable_golds < $this->singleGolds) ? - 1 : 0;
	}
	
	private function _checkMaxTimes() {
		$times = D ( 'Record' )->oneDayLottoTimes ( $this->User->u_id );
		return ($times < $this->maxTimes) ? 0 : - 2;
	}
	
	# 抽奖
	private function onLotto() {
		$this->_setItems ();
		$this->_getPrize ();
		$this->_getRealPrize ();
	}
	
	private function _setItems() {
		$lottolist = A ( 'Lotto' )->_getAll ();
		$this->items = array (array ('id' => 0, 'num' => 0, 'name' => '可惜未中奖', 'prst_num' => 1, 'count' => - 1 ) );
		foreach ( $lottolist as $k => $v ) {
			$probability_num += $v ['lo_probability'] * $this->accurate;
			$this->items [] = array ('id' => $v ['lo_id'], 'num' => $probability_num, 'name' => $v ['lo_name'], 'prst_num' => $v ['prst_num'], 'count' => $v ['lo_count'] );
		}
	}
	
	private function _getPrize() {
		$rand = rand ( 1, $this->accurate * 100 );
		foreach ( $this->items as $k => $v ) {
			if (isset ( $this->items [$k - 1] )) {
				if ($rand > $this->items [$k - 1] ['num'] && $rand <= $this->items [$k] ['num']) {
					$this->prize = ($v ['count'] == - 1 || $v ['count'] > 0) ? $v : $this->items ['0'];
				}
			}
		}
		if(!$this->prize) $this->prize = $this->items ['0'];
	}
	
	private function _getRealPrize(){
		# 对于已经获取过实物奖品的用户，再次获取实物奖品时取消奖品，设为未中奖
		if($this->prize['id']>0 && $this->prize['count']>0){
			$_rep = array('rep_type'=>'lotto', 'u_id'=>$this->User->u_id);
			$rep = M('RecordPrize')->where($_rep)->find();
			if($rep){
				$this->prize = array('id' => 0, 'num' => 0, 'name' => '可惜未中奖', 'prst_num' => 1, 'count' => - 1 );
			}
		}
	} 
	
	# 写抽奖日志，记录抽奖
	private function onRecord() {
		import('@.Util.UserGolds');
		# 扣去用户抽奖消耗金币
		$lotto_params = array('reg_gold'=> -$this->singleGolds, 'reg_description'=>'抽奖消耗金币：'.$this->singleGolds);
		$lotto_userGolds = new UserGolds($this->User, $lotto_params);
		$lotto_userGolds->save();
		
		$this->User->u_golds = $this->User->u_golds - $this->singleGolds; # 实时改变用户信息
		
		# 1.记录抽奖日志
		$_reldata = array ('u_id' => $this->User->u_id, 'lo_id' => $this->prize ['id'], 'rel_time' => time () );
		$_reldata ['rel_description'] = ($this->prize ['id'] > 0) ? '抽中奖品：' . $this->prize ['name'] : $this->prize ['name'];
		D('Record')->onAdd('Lotto', $_reldata);
		
		if ($this->prize ['id'] > 0) {
			if ($this->prize ['count'] == - 1) {
				# 库存不限：奖品为金币，记录金币日志
				
				$prize_params = array('reg_gold' => $this->prize ['prst_num'], 'reg_description' => '抽中奖品：'.$this->prize ['name'].', 金币+'.$this->prize ['prst_num']);
				$prize_userGolds = new UserGolds($this->User, $prize_params);
				$prize_userGolds->save();
				
				$this->User->u_golds = $this->User->u_golds + $this->prize ['prst_num']; # 实时改变用户信息
			} else {
				# 奖品为实物，记录奖品日志
				$_repdata = array('rep_type' => 'lotto', 'u_id' => $this->User->u_id, 'prst_id' => $this->prize['id'], 'rep_time' => time () );
				$_repdata ['rep_description'] = '抽中奖品：'.$this->prize ['name'];
				D('Record')->onAdd('Prize', $_repdata);
				
				# 扣去库存
				$_lo['lo_count'] = $this->prize['count'] - 1;
				M('PrizeLotto')->where(array('lo_id'=>$this->prize['id']))->data($_lo)->save();
			}
		}
		
		$this->_return(1);
	
	}
	
	private function prizeToLocation(){
		$lotto_prize = S("lotto_prize");
		
		$lo_id = $this->prize['id']>0 ?$this->prize['id'] :0 ;
		
		foreach($lotto_prize as $k=>$v){
			foreach($v as $key=>$val){
				if($lo_id == $val['lo_id']){
					return $val['location'];
				}
			}
		}
	}
	
	# 返回信息
	private function _return($status) {
		$this->_rstatus = $status;
		$location = $this->prizeToLocation($this->prize['id']);
		if($this->_rstatus != 0){
			$_rdata = array(
							'status' => $this->_rstatus, 
							'prize_id'=>$this->prize['id'], 
							'msg' => $this->_description [$this->_rstatus], 
							'location' => $location, 
							'more'=>$this->prize['name'],
							'data'=>array( 'isavailable' => (boolean) $this->isAvailable , 'item' => $location, 'info' => $this->_description [$this->_rstatus] , 'frame' => $frame )
			);
			die(json_encode($_rdata));
		}
	}

}

?>